// pages/shopping/shop.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    islogin: false,
    activecolor:'#D32E1E',
    color:'#ffffff',
    circular:true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    filterBox:false,
    select:1,
    banners:'',
    types:'',
    list:'',
    typeid:'',
    section:'',
    price_start:'',
    price_end:'',


    // 滚动加载
    currentpage: 1,
    nogd: false,
    canjiazai: true
  },
  // 授权登录
  bindGetUserInfo(e) {
    var that = this;
    if (wx.getStorageSync("uqid")) {
      this.setData({
        islogin: true
      })
    } else {
      if (e.detail.userInfo) {
        wx.setStorageSync("nickName", e.detail.userInfo.nickName)
        wx.setStorageSync("avatarUrl", e.detail.userInfo.avatarUrl)
        console.log()
        // 用户按了允许授权按钮
        var code = wx.getStorageSync("code");
        ajax('/api/user/login', {
          data: {
            code: code,
            nickname: e.detail.userInfo.nickName,
            avatar: e.detail.userInfo.avatarUrl,
          },
          header: {
            'Accept': 'application/x-www-form-urlencoded', // 默认值
          },
          method: "put",
        }, res => {
          wx.setStorageSync("uqid", res.data.uqid)
          this.setData({
            islogin: true
          })
        })
      } else {
        wx.showToast({
          title: '请先授权！',
          icon: 'none',
          duration: 2000
        })
      }
    }

  },
  // 获取轮播图
  getbanner: function () {
    ajax('/api/index/banner', {
      data: {
        type: 2
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      this.setData({
        banners: res.data.list
      })
    })
  },
  // 获取类型
  gettypes:function(){
    ajax('/api/index/style_list', {
      data: {
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      this.setData({
        types: res.data
      })
    })
  },

  totop: function (e) { // 一键回到顶部
    this.setData({
      topNum: this.data.topNum = 0
    });
  },


  lower: function () {

    if (this.data.canjiazai) {
      this.setData({
        canjiazai: false
      })
      if (this.data.nogd == false) {
        var currentpage = this.data.currentpage
        console.log(currentpage)

        this.getshop(currentpage + 1)

      } else {
        console.log("没有更多")
      }

    } else {
      console.log("不能加载")
    }
  },


  // 获取数据
  getshop:function(page){
    ajax('/api/index/good_list', {
      data: {
        page:page,
        limit:6,
        style_id: this.data.typeid,
        price_start: this.data.price_start,
        price_end: this.data.price_end,
        new:this.data.select
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {

      if (page == 1) {
        if (res.data.length > 0) {


          this.setData({
            list: res.data
          })
        } else {

          this.setData({
            nogd: true,
            list: res.data
          })
        }
        if (res.data.length < 6 && res.data.length > 0) {
          this.setData({
            nogd: true,
          })
        }

      } else {

        var list = this.data.list

        if (res.data.length > 0) {

        

          for (var x in res.data) {
            list.push(res.data[x])
          }
          this.setData({
            list: list,
            currentpage: this.data.currentpage + 1,
            canjiazai: true,
          })
        } else {
          console.log(res.data.length)
          this.setData({
            nogd: true,
          })
        }
        if (res.data.length < 6 && res.data.length > 0) {
          this.setData({
            nogd: true,
          })
        }
      }
    })
  },
  // 选择
  active: function (e) {
    var idx = e.currentTarget.dataset.index;
    this.setData({
      select: idx
    })
    


    this.setData({
      currentpage: 1,
      nogd: false,
      order: 111,
      canjiazai: true
    })
    this.totop()

    this.getshop(1)


  },
  // 筛选类型id
  typeid:function(e){
    var id = e.currentTarget.dataset.typeid;
    this.setData({
      typeid: id
    })
  },
  // 筛选区间
  section:function(e){
    // console.log(e)
    var section = e.currentTarget.dataset.section;
    var sections = e.currentTarget.dataset.sections;
    var arr = sections.split('-')
    console.log(arr,'arr')
    if(arr.length == 1){
      if(arr[0] == '500以内'){
        this.setData({
          section: section,
          price_end: 500,
          price_start: ''
        })
      }else{
        this.setData({
          section: section,
          price_start: 50000,
          price_end: ''
        })
      }
    }else{
      this.setData({
        section: section,
        price_start: arr[0],
        price_end: arr[1]
      })
    }
    
  },
  // 显示筛选框
  filterBox:function(){
    this.setData({
      filterBox:true
    })
  },
  // 完成筛选框
  finish:function(){
    this.setData({
      filterBox: false
    })
    this.setData({
      currentpage: 1,
      nogd: false,
      canjiazai: true
    })
    this.totop()

    this.getshop(1)

  },
  // 重置
  reset:function(){
    this.setData({
      typeid: '',
      section:'',
      price_start:'',
      price_end:''
    })
  },
  //  类型跳转页面
  typeroute:function(e){
    var id = e.currentTarget.dataset.typeid
    wx.navigateTo({
      url: '/pages/shop-pendant/pendant?style_id=' + id,
    })
  },
  // 商品详情页面
  details:function(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/shop-details/details?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getbanner()
    this.gettypes()

    

    this.getshop(1)
    if (wx.getStorageSync("uqid")) {
      this.setData({
        islogin: true
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


    this.setData({
      currentpage: 1,
      nogd: false,
    })

    this.getshop(1)

    if (wx.getStorageSync("uqid")) {
      this.setData({
        islogin: true
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})